﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class tb_User_Service : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e) 
        {
            if (!IsPostBack) { Session["folder"] = "tb_User_Service";
                BindGrid(); 
            } 
        }
        private void BindGrid() { 
            grdtb_User_Service.DataSource = tb_User_ServiceService.tb_User_Service_GetByAll(); 
            grdtb_User_Service.DataBind(); 
        }
        protected void btnThem_Click(object sender, EventArgs e) 
        { 
            pnShow.Visible = false; 
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this); 
            Session["upload"] = null;
            Insert = true;
        }
        protected void btnXoa_Click(object sender, EventArgs e) {
            try {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtb_User_Service.Items.Count; i++) {
                    item = grdtb_User_Service.Items[i]; 
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item) 
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked) { 
                            string strId = item.Cells[1].Text; 
                            try {
                                
                                //tb_User_ServiceService.tb_User_Service_Delete(strId); 
                            }
                            catch {
                            } 
                        } 
                    } 
                }
                grdtb_User_Service.CurrentPageIndex = 0;
                BindGrid();
            } catch { 
            } 
        
        }
        protected void grdtb_User_Service_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tb_User_ServiceInfo strCA = new tb_User_ServiceInfo();
            strCA.Username = e.CommandArgument.ToString();
            if (e.CommandName == "Edit") { 
                DataTable dt = tb_User_ServiceService.tb_User_Service_GetById(strCA);
                txtUsername.Text = dt.Rows[0]["Username"].ToString();
                dllServiceId_Update.SelectedValue = dt.Rows[0]["ServiceId"].ToString(); 
                pnUpdate.Visible = true; pnShow.Visible = false;
            }
            else { 
                tb_User_ServiceService.tb_User_Service_Delete(strCA);
                BindGrid();
            }
        }
        
        protected void lblBack_Click(object sender, EventArgs e) { 
            pnUpdate.Visible = false; 
            pnShow.Visible = true;
            Insert = false; 
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]
            if (txtUsername.Text.Trim().Equals("")) { 
                WebMsgBox.Show("Username không thể trống !");
                txtUsername.Focus();
                return; 
            } 
            if (dllServiceId_Update.SelectedValue.Trim().Equals("")) 
            {
                WebMsgBox.Show("ServiceId không thể trống !");
                
                dllServiceId_Update.Focus();
                return;
            }
            #endregion
            Data.tb_User_ServiceInfo obj = new Data.tb_User_ServiceInfo(); 
            obj.Username = txtUsername.Text; 
            obj.ServiceId = dllServiceId_Update.SelectedValue; 
            if (Insert == true) { 
                tb_User_ServiceService.tb_User_Service_Insert(obj);
            } else
            { 
                tb_User_ServiceService.tb_User_Service_Update(obj); 
            } 
            BindGrid();
            pnShow.Visible = true; 
            pnUpdate.Visible = false;
            Insert = false;
        }
        
        protected void grdtb_User_Service_PageIndexChanged(object source, DataGridPageChangedEventArgs e) { 
            grdtb_User_Service.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        private void ShowDll()
        {
            var dt = new DataTable();
            dt = tb_ServicesService.tb_Services_GetByAll();
            dllServiceId.DataSource = dt;
            dllServiceId.DataTextField = ""; 
            dllServiceId.DataValueField = "";
            dllServiceId.DataBind();
            dllServiceId_Update.DataSource = dt; 
            dllServiceId_Update.DataTextField = "";
            dllServiceId_Update.DataValueField = "";
            dllServiceId_Update.DataBind();

        }
        
        protected void btnshow_Click(object sender, EventArgs e) {
            grdtb_User_Service.DataSource = tb_User_ServiceService.tb_User_Service_GetByServiceId(dllServiceId.SelectedValue); 
            
            grdtb_User_Service.DataBind(); 
        }

        protected void grdtb_User_Service_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType; if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll"); if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtb_User_Service.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")"); ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}